package com.xxxx.mytest.jihe.listTest

import scala.collection.mutable.ListBuffer

/**
 * @program: day0303
 * @description: 尽信书，则不如无书
 * @author: CoreDao
 * @create: 2021-03-03 10:46
 **/

object ListTest1 {
  def main(args: Array[String]): Unit = {

    val list: List[Int] = List(1, 2, 3)
    //高阶函数的应用
    list.foreach(println)
    for (i <- list){
      print(i)
    }

    /**
     * filter过滤，不会改变原有的数据结构
     */
    list.filter(x=>true).foreach(println)
    list.filter(x=>{
      x>1 && x%2==0
    }).foreach(println)


    /**
     * count
     * 统计符合条件数据的个数
     */
    println(list.count(_ => true))
    println("-----------------")
    /**
     * map & flatMap
     * 映射
     * 脱敏：如身份证中间变成***
     * map:一进一出
     * flatMap:一进可能多出
     */

    val list1 = list.map(x => {
      x * 2
    })
    list1.foreach(println)

    val list2: List[String] = List("hello shanghai", "hello beijing", "hello guangzhou")
    val list3: List[Array[String]] = list2.map(x => {
      x.split(" ")
    })
    list3.foreach(println)
    println("++++++")
    list3.foreach(x=>{
      x.foreach(println)
      println("======")
    })

    val list4 = list2.flatMap(x => {
      x.split(" ")
    })
    list4.foreach(println)
    println("+++++++++++++++++")

    /**
     * 可变长度list
     */
    val listBuffer = ListBuffer[Int](1, 2, 3, 4, 5)
    listBuffer.append(6,7,8)
    listBuffer.+=:(0)
    listBuffer.+=(7)
    listBuffer.foreach(println)
  }

}
